package com.situ.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Coach;
import com.situ.gym.pojo.query.CoachQuery;
import com.situ.gym.service.ICoachService;
import com.situ.gym.util.JwtUtil;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/coach")
public class CoachController {
    @Autowired
    private ICoachService coachService;

    //查找所有教练，用于作为课程外键时供挑选
    @GetMapping("/selectAll")
    public Result<List<Coach>> selectAll() {
        List<Coach> list = coachService.list();//mybatisplus提供的查找所有方法
        return Result.ok(list);
    }

    //Restful    GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
    //@RequestMapping("/list")
    //@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    //@RequestMapping(value = "/list", method = RequestMethod.GET)
    @GetMapping("/list")
    //分页查找
    //Result:是自定义返回类型状态码code、消息msg 和 数据data等字段
    public Result list(CoachQuery coachQuery) {//Restful风格，只能发get
        System.out.println("CoachController.list");
        IPage<Coach> page = coachService.list(coachQuery);//自己写的分页查找方法
        return Result.ok(page);
    }

    //按id删除
    // /coach/deleteById?id=1
    // /coach/deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        coachService.removeById(id);
        return Result.ok("删除成功");
    }

    //批量删除
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {//Variable变量
        //使用mp
        //coachService.removeBatchByIds(Arrays.asList(ids));

        //自己写
        coachService.deleteAll(ids);
        return Result.ok("删除成功");
    }


   /* @PostMapping("/add")
    public Result add(Coach coach) {
        System.out.println(coach);
        return Result.ok();
    }*/

    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回前台
    //添加
    @PostMapping("/add")
    public Result add(@RequestBody Coach coach) {
        System.out.println(coach);
        //简单的增删改查使用mp,不用自己写
        coachService.save(coach);
        return Result.ok("添加成功");
    }

    //更新
    //更新前回显数据
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Coach coach = coachService.getById(id);//根据前台传来的参数id查询数据库，返回对应的 Coach 对象
        return Result.ok(coach);
    }

    //执行更新
    @PutMapping("/update")
    public Result update(@RequestBody Coach coach) {
        System.out.println(coach);
        //使用mp
        coachService.updateById(coach);//有选择的更新，前台传来值的属性（列）就更新，没有值保持原来的
        //coachService.saveOrUpdate(coach);//添加更新放一起的写法
        return Result.ok("更新成功");
    }

    //状态更新
    @PutMapping("/updateStatus/{id}/{status}")
    private Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        Coach coach = new Coach();
        coach.setId(id);
        coach.setStatus(status);
        coachService.updateById(coach);//有选择的更新，前台传来值的属性（列）就更新，没有值保持原来的
        return Result.ok("更新成功");
    }
}

